package com.weiyi.mashangzhu.Roomer.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.IsPhoneNum;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONException;
import org.json.JSONObject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ChangePhoneActivity extends BaseActivity {

    @BindView(R.id.change_phone)
    EditText changePhone;
    @BindView(R.id.change_verify_code)
    EditText changeVerifyCode;
    @BindView(R.id.change_get_code)
    Button changeGetCode;
    int time = 60;
    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                if (time >= 2) {
                    time -= 1;
                    changeGetCode.setText("稍后" + time + "s...");
                    handler.sendEmptyMessageDelayed(1, 1000);
                    changeGetCode.setEnabled(false);
                } else {
                    time = 60;
                    changeGetCode.setTextColor(getResources().getColor(R.color.colorPrimary));
                    changeGetCode.setEnabled(true);
                    changeGetCode.setText("获取验证码");
                }
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_phone);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.change_get_code, R.id.change_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.change_get_code:
                if (IsPhoneNum.isMobileNO(changePhone.getText() + "") == false) {
                    T.showShort(this, "请输入有效手机号码");
                } else {
                    OkHttpUtils.get().url(API.BaseApi_R + API.SendSysCode_Api)
                            .addParams("users.uAccount", changePhone.getText().toString().trim())
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    T.showShort(ChangePhoneActivity.this,"数据获取失败，请稍后再试");

                                }

                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONObject jsonObject = new JSONObject(response);
                                        String status = jsonObject.getString("status") + "";
                                        if (status.equals("0")) {
                                            changeGetCode.setTextColor(getResources().getColor(R.color.colorGrey));
                                            changeGetCode.setText("稍后" + time + "s...");
                                            handler.sendEmptyMessageDelayed(1, 1000);
                                        }else {
                                            T.showShort(ChangePhoneActivity.this,jsonObject.getString("msg"));
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                            });

                }
                break;
            case R.id.change_confirm:
                if ((changePhone.getText() + "").equals("")) {
                    T.showShort(this, "手机号不能为空");
                } else {
                    if (IsPhoneNum.isMobileNO(changePhone.getText() + "") == false) {
                        T.showShort(this, "请输入有效手机号码");
                    } else {
                        if ((changeVerifyCode.getText() + "").equals("")) {
                            T.showShort(this, "短信验证码不能为空");
                        } else {
                            LoadingDialog.show(this);
                            OkHttpUtils.get()
                                    .addParams("token", SPUtils.get(this,"token","")+"")
                                    .addParams("newPhone",(changePhone.getText()+"").trim())
                                    .addParams("codes",(changeVerifyCode.getText()+"").trim())
                                    .url(API.BaseApi_R+API.ChangePhone)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            LoadingDialog.dismiss(ChangePhoneActivity.this);
                                            T.showShort(ChangePhoneActivity.this,"数据获取失败，请稍后再试");
                                        }

                                        @Override
                                        public void onResponse(String response) {
                                            Log.e("cppppppp","re="+response);
                                            LoadingDialog.dismiss(ChangePhoneActivity.this);
                                            try {
                                                JSONObject jsonObject=new JSONObject(response);
                                                if (jsonObject.getInt("status")==0){
                                                    SPUtils.put(ChangePhoneActivity.this,"token",jsonObject.getString("data"));
                                                    finish();
                                                }
                                                T.showShort(ChangePhoneActivity.this,jsonObject.getString("msg"));
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }

                                        }
                                    });
                        }
                    }
                }
                break;
        }
    }
}
